/*流程部署*/
$(function load() {
    searchProcess();
})

function deploy() {
    var bpmnName = $("#bpmnName").val();
    $.ajax({
        type: "POST",
        url: "/processDefinition/deploy",
        data: {bpmnName: bpmnName},
        success: function (data) {
            if (data.msg == 'success') {
                alert("流程部署成功")
            } else {
                alert("流程部署失败")
            }
        }

    })
}

function start() {
    var applyUserId = $("#name").val();
    var day = $("#day").val();
    var reason = $("#reason").val();
    var user1 = $("#user1").val();
    var user2 = $("#user2").val();
    var instanceKey = $("#instanceKey").val();

    var s = {
        "instanceKey": instanceKey,
        "applyUserId": applyUserId,
        "variables": {'day': day, 'reason': reason, 'user1': user1, 'user2': user2},
    }

    $.ajax({
        type: "POST",
        url: "/processDefinition/startProcessInstance",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(s),
        success: function (data) {
            if (data.msg == 'success') {
                alert("流程启动成功")
            } else {
                alert("流程启动失败")
            }
        }
    })


}

function searchProcess() {
    var type = $("#type").val();
    $.ajax({
        type: "GET",
        url: "/processDefinition/processDefinitionByType",
        data: {
            type: type
        },
        success: function (data) {
            if (data.msg == 'success') {
                $("#processContent").empty();
                var html = "";
                if (data.list.length < 1) {
                    html += '<tr><td colspan="8">暂无流程部署</td></tr>';
                }
                $.each(data.list, function (k, v) {
                    html +=
                        '<tr>' +
                        '<td>' + v.type + '</td>' +
                        '<td>' + v.id + '</td>' +
                        '<td>' + v.deploymentId + '</td>' +
                        '<td>' + v.key + '</td>' +
                        '<td>' + v.name + '</td>' +
                        '<td>' + v.version + '</td>' +
                        '<td>' + v.description + '</td>' +
                        '<td>' +
                        '<button type="button" onclick="startProcess(\''+v.key+'\')">开启流程</button>' +
                        '<button type="button" onclick="exports(\''+v.key+'\')">导出流程</button>' +
                        '</td></tr>';
                });
                $("#processContent").append(html);
            } else {
                alert(data.msg)
            }
        }
    })
}

function startProcess(processKey) {
    var key = processKey;
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.open({
            type: 1,
            title: "流程参数信息",
            area: ['900px', '500px'], //宽高
            content: '<div>\n' +
                '    <table border="0">\n' +
                '        <tr>\n' +
                '            <td colspan="2">节点表单参数</td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>申请人：</td>\n' +
                '            <td><input type="text" id="name"/></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>请假天数：</td>\n' +
                '            <td><input type="number" id="day"/></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>请假原因：</td>\n' +
                '            <td><input type="text" id="reason"/></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td colspan="2">节点候选人</td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>第一个节点候选人：</td>\n' +
                '            <td><input type="text" placeholder="多个候选人之间用逗号隔开" id="user1"/></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>第二个节点候选人：</td>\n' +
                '            <td><input type="text" placeholder="多个候选人之间用逗号隔开" id="user2"/></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td colspan="2">流程参数</td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td>流程实例名称：</td>\n' +
                '            <td><input type="text" id="instanceKey" value="'+key+'"/></td>\n' +
                '        </tr>\n' +
                '        <tr>\n' +
                '            <td colspan="2">\n' +
                '                <button onclick="start()">开启流程</button>\n' +
                '            </td>\n' +
                '        </tr>\n' +
                '    </table>\n' +
                '</div>'
        });
    });
}

function exports(processKey) {
    window.location="/processDefinition/export?key="+processKey;
}